package cartagows.util;

import java.util.HashMap;

import org.apache.axis2.addressing.EndpointReference;

import cartago.ArtifactId;
import cartago.CartagoException;
import cartago.ICartagoContext;
import cartago.Op;
import cartago.OpFeedbackParam;
import cartago.WorkspaceId;
import cartago.security.AgentIdCredential;
import cartago.util.agent.Agent;
import cartagows.CartagowsParam;
import cartagows.WSMsgInfo;
/**
 * Class that represent a generic worker. This class implements a set of primitives
 * that allow the Worker to join a workspace, use an artifact, send messages through WSInterface artifacts
 * and so on.
 * @author Andrea Santi
 *
 */
public class WSAgent extends Agent{ 

	protected String opName;
	protected HashMap<String,ICartagoContext> openContexts;

	protected ICartagoContext currentContext;
	protected WorkspaceId wspId;

	public WSAgent(String agentName) throws CartagoException{
		super(agentName);
		this.joinWorkspace("web-services", new AgentIdCredential(agentName));
	}

	public WSAgent(String agentName, String workspaceName, String workspaceHost) {
		super(agentName, workspaceName, workspaceHost);
	}
	
	protected ArtifactId createWSPanel(String localName, String wsdlURI, String serviceName) throws CartagoException {
		try {
			return makeArtifact(localName, CartagowsParam.WSPANEL_CLASS_PATH, 
					new Object[]{wsdlURI, serviceName});
		} catch (Exception ex){
			ArtifactId aid = lookupArtifact(localName);
			disposeArtifact(aid);
			return makeArtifact(localName, CartagowsParam.WSPANEL_CLASS_PATH, 
					new Object[]{wsdlURI, serviceName});
		}
	}

	protected ArtifactId createWSInterface(String localName, String wsdlURI, String serviceName, String port) throws Exception {
		try {
			return makeArtifact(localName, CartagowsParam.WSINTERFACE_CLASS_PATH, 
					new String[]{wsdlURI, serviceName, port});
		} catch (Exception ex){
			ArtifactId aid = lookupArtifact(localName);
			disposeArtifact(aid);
			return makeArtifact(localName, CartagowsParam.WSINTERFACE_CLASS_PATH, 
					new String[]{wsdlURI, serviceName, port});
		}
	}

	protected ArtifactId createWSInterface(String localName, String wsdlURI) throws CartagoException {
		try {
			return makeArtifact(localName, CartagowsParam.WSINTERFACE_CLASS_PATH, 
					new Object[]{wsdlURI});
		} catch (Exception ex){
			ArtifactId aid = lookupArtifact(localName);
			disposeArtifact(aid);
			return makeArtifact(localName, CartagowsParam.WSINTERFACE_CLASS_PATH, 
					new Object[]{wsdlURI});
		}
	}

	protected ArtifactId createWSInterface(String localName, String wsdlURI, String portType, EndpointReference targetEpr) throws CartagoException {
		try {
			return makeArtifact(localName, CartagowsParam.WSINTERFACE_CLASS_PATH, 
					new Object[]{wsdlURI, portType, targetEpr, true});
		} catch (Exception ex){
			ArtifactId idArt = lookupArtifact(localName);
			disposeArtifact(idArt);
			return makeArtifact(localName, CartagowsParam.WSINTERFACE_CLASS_PATH, 
					new Object[]{wsdlURI, portType, targetEpr, true});
		}
	}

	protected String doOneWay(ArtifactId wsProxy, String opName, String msg, int timeout) throws CartagoException{
		OpFeedbackParam<String> msgId = new OpFeedbackParam<String>();
		doAction(wsProxy, new Op("oneWayOp", opName, msg, msgId), timeout);
		return msgId.get();
	}

	protected void getWSReply(ArtifactId wsProxy, String msgId, OpFeedbackParam<WSMsgInfo> res, OpFeedbackParam<String> resId, int timeout)throws CartagoException{
		doAction(wsProxy, new Op("getWSReply", msgId, res, resId), timeout);
	}

	protected WSMsgInfo doRequestResponse(ArtifactId wsProxy, String opName, String msg, int timeout) throws CartagoException {
		OpFeedbackParam<WSMsgInfo> response = new OpFeedbackParam<WSMsgInfo>();
		OpFeedbackParam<String> responseId = new OpFeedbackParam<String>();
		doAction(wsProxy, new Op("requestOp", opName, msg, response, responseId), timeout);
		return response.get();
	}
	
	protected WSMsgInfo doRequestResponse(ArtifactId wsProxy, String opName, String msg) throws CartagoException {
		OpFeedbackParam<WSMsgInfo> response = new OpFeedbackParam<WSMsgInfo>();
		OpFeedbackParam<String> responseId = new OpFeedbackParam<String>();
		doAction(wsProxy, new Op("requestOp", opName, msg, response, responseId), -1);
		return response.get();
	}
}